package fs.component {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;
	import fs.component.event.ComponentEvent;
	
	/**
	 * ...
	 * @author F.S.
	 */
	public class RadioButton extends Sprite {
		internal var buttonGroupIndex:String;
		
		private var _circle:Sprite;
		private var _point:Sprite;
		private var _tfContent:TextField;
		
		private var _enabledLayer:Sprite;
				
		private var _isOn:Boolean;
		private var _isEnabled:Boolean = true;
		private var _title:String = "";
		private var _groupName:String = "";		
		
		public var onChanged:Function;
		
		public function RadioButton() {
			addUi();
			addEvent();
		}
		
		internal function setOn(value:Boolean):void {
			_isOn = value;
			_point.visible = value;
		}
		
		private function addEvent():void {
			addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(e:MouseEvent):void {
			if (!_isEnabled) return;
			isOn = !isOn;
			if (null != onChanged) {
				var event:ComponentEvent = new ComponentEvent(Event.CHANGE,this);
				onChanged.apply(null, [event]);
			}
		}
		
		private function addUi():void {
			_enabledLayer = new Sprite();
			addChild(_enabledLayer);
			
			_circle = new Sprite();
			_circle.graphics.lineStyle(0.5);
			_circle.graphics.beginFill(0xffffff);
			_circle.graphics.drawCircle(5, 10, 5);
			_circle.graphics.endFill();
			_enabledLayer.addChild(_circle);
			
			_point = new Sprite();
			_point.graphics.lineStyle(0);
			_point.graphics.beginFill(0);
			_point.graphics.drawCircle(5, 10, 3);
			_point.graphics.endFill();
			_point.visible = false;
			_enabledLayer.addChild(_point);
			
			_tfContent = new TextField();
			_tfContent.x = 12;
			_tfContent.autoSize = TextFieldAutoSize.LEFT;
			_tfContent.selectable = false;
			_enabledLayer.addChild(_tfContent);
			
			buttonMode = true;
		}
		
		public function get isOn():Boolean {
			return _isOn;
		}
		
		public function set isOn(value:Boolean):void {
			ButtonGroup.setOthersOffInGroup(this);
		}
		
		public function get isEnabled():Boolean {
			return _isEnabled;
		}
		
		public function set isEnabled(value:Boolean):void {
			_enabledLayer.filters = value?[]:[new ColorMatrixFilter([1,  0,  0, 0,0x80,
																	 0,  1,  0, 0,0x80,
																	 0,    0,1, 0,0x80,
																	 0,    0,  0, 1,0 ])];
			_isEnabled = value;
		}
		
		public function get title():String {
			return _title;
		}
		
		public function set title(value:String):void {
			_title = value;
			_tfContent.text = title;
		}
		
		public function get groupName():String {
			return _groupName;
		}
		
		public function set groupName(value:String):void {
			ButtonGroup.remove(this);
			
			_groupName = value;
			ButtonGroup.add(this);
		}
		
		public static function getSelectedInGroup(groupName:String):RadioButton {
			return ButtonGroup.getSelectedInGroup(groupName);
		}
	}

}